//Hassan Al-Khailfah 201328530
//Mohammed Alrashidi 201318090
//project phase 1
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define IN 1
#define OUT 0
void Addbook();
void Displaybook();
void Searchbook();
void Editbook();
void Deleteabook();
void Exit();
char info[500];
struct 
{
  int bid;
  char bname[25] ;
  char author[25];
  int nooftitles;
  char titles[500];
  int status;
}book;
FILE *librecord;
int main()
{        
    int choice,i;
    printf("****Welcome to Book Mangement System****");
    do{
   printf("\n\n MAIN MENU\n");
   printf("======================");
   printf("\n[1] Add a new Book");
   printf("\n[2]List all books");
   printf("\n[3] Search for book");
   printf("\n[4] Edit a book");
   printf("\n[5]Delete a book");
   printf("\n[0] Exit");
   printf("\n======================\n");
   printf("Enter the choice:  ");
    scanf("%d",&choice);  
    switch (choice)
    {
    case 1:
     Addbook(); 
     break;
	case 2:
     Displaybook();
     break;
    case 3:
     Searchbook();
      break;
    case 4:
     Editbook();
      break;	  
    case 5:
    Deleteabook();
    break;
    case 0:
      Exit();
     default:
    printf(" ! Invalid Input...\n");
    }
}while(choice!=0);
 return (0);
}

void Addbook() //function if add new book
{ 
int i;book.status=IN;
            //opening the librecord file
librecord = fopen("librecord.txt","a+");//a+Opens for reading and appending 
printf("Enter The uniqueid of The Book :(Integer) \n");
 scanf("%d",&book.bid);//the book number
printf("Enter The Name of The Book :\n");
scanf("%s",book.bname);//%sprints characters until the null character is reached
printf("Enter The Name of Author :\n");
scanf("%s",book.author);//the name of auther
printf("Enter The Number of Titles Of The Book:(Integer)\n");
scanf("%d",&book.nooftitles);//number of titles inside the book
//to printf the information inside the file
fprintf(librecord,"\n%d\t%s\t%s\t%d\t%d\t",book.bid,book.bname,book.author,book.status,book.nooftitles);
printf("Enter The Titles Of The Book : \n");//to recored the titles//\t to introduce new  tab 
 for(i=0;i<book.nooftitles;i++)//to read rach title alone
    {
 scanf("%s",book.titles);
fprintf(librecord,"%s\t",book.titles); //to print the title inside file
    }
fclose(librecord);// to close the file
printf(" (' ' ) A New Book has been Added Successfully...\n");//to infome the user 	
}

void Displaybook()
{
	librecord = fopen("librecord.txt","a+");
    printf("\nBookid\tName\tAuthor\tStatus\tNo.\tTitles\n",info);
    do
    {
        fgets(info,500,librecord);
        printf("%s\n",info);
    }while(!feof(librecord));
    fclose(librecord);
}

void Searchbook()
{int i,Target;
    char stats[3];
    int Found=0;
    if((librecord=fopen("librecord.txt","r"))==NULL)
        printf(" ! The File is Empty...\n\n");
    else
    {
        printf("\nEnter The ID Of Book : ");
            scanf("%d",&Target);
        while(!feof(librecord)&& Found==0)
        {
        fscanf(librecord,"%d %s %s %d %d", &book.bid,book.bname,book.author,&book.status,&book.nooftitles);
            if(Target==book.bid)
                Found=1;
            for(i=0;i<book.nooftitles;i++)
                fscanf(librecord,"%s",book.titles); 
        }
        if(Found)
        {
            if(book.status==IN)
                strcpy(stats,"IN");
            else
                strcpy(stats,"OUT");
            
            printf("\nThe Unique ID of The Book:  %d\nThe Name of Book is:  %s\nThe Author is:  %s\nThe Book Status:%s\n\n",book.bid,book.bname,book.author,stats);
            }
        else if(!Found)
            printf("! There is no such Entry...\n");
        fclose(librecord);
    }

	}

void Editbook()
{

int i;
    char stats[3];
    int Target;
    int id,noT,stat;
    char name[25], author[25];
    FILE* temp = fopen("temp.txt","w");
    int found=0;
    if((librecord=fopen("librecord.txt","r"))==NULL)
        printf(" ! The File is Empty...\n\n");
    else
    {
        printf("\nEnter The ID Of Book : ");
            scanf("%d",&Target);
        while(!feof(librecord)){
        fscanf(librecord,"%d\t%s\t%s\t%d\t%d", &book.bid,&book.bname,&book.author,&book.status,&book.nooftitles);
            for(i=0;i<book.nooftitles;i++)
                fscanf(librecord,"%s",&book.titles);
        
		if(Target==book.bid){
			found = 1;
			//printf("%d\t%s\t%s\t%d\t%d", book.bid,book.bname,book.author,book.status,book.nooftitles);		
			printf("Enter The uniqueid of The Book :(Integer) \n");
			scanf("%d",&id);
			printf("Enter The Name of The Book :\n");
			scanf("%s",&name);
			printf("Enter The Name of Author :\n");
			scanf("%s",&author);
			printf("Enter The Number of Titles Of The Book:(Integer)\n");
			scanf("%d",&noT);
			
			fprintf(temp,"\n%d\t%s\t%s\t%d\t%d\t",id,name,author,1,noT);
			printf("Enter The Titles Of The Book : \n");
			char titles[25];
			 for(i=0;i<noT;i++)
			    {
				scanf("%s",&titles);
				fprintf(temp,"%s\t",titles); 
				}
			}
			else{
				fprintf(temp,"\n%d\t%s\t%s\t%d\t%d\t", book.bid,book.bname,book.author,book.status,book.nooftitles);
            	for(i=0;i<book.nooftitles;i++)
            		fprintf(temp,"%s\t",book.titles); 
         	}
         	    fscanf(librecord,"%d\t%s\t%s\t%d\t%d", &book.bid,&book.bname,&book.author,&book.status,&book.nooftitles);
	
        }
        fclose(librecord);
        fclose(temp);
        remove("librecord.txt");
		rename("temp.txt","librecord.txt");

    }


}
void Deleteabook()
{
    int i;
    char stats[3];
    int Target;
    int id,noT,stat;
    char name[25], author[25];
    FILE* temp = fopen("temp.txt","w");
    int found=0;
    if((librecord=fopen("librecord.txt","r"))==NULL)
        printf(" ! The File is Empty...\n\n");
    else
    {
        printf("\nEnter The ID Of Book : ");
            scanf("%d",&Target);
        while(!feof(librecord)){
        fscanf(librecord,"%d\t%s\t%s\t%d\t%d", &book.bid,&book.bname,&book.author,&book.status,&book.nooftitles);
            for(i=0;i<book.nooftitles;i++)
                fscanf(librecord,"%s",&book.titles);
        
		if(Target==book.bid){
			int pass = 123456,password;
			printf("Enter the pass");
			scanf("%d",&password);
			if(pass!=password){
				printf("wrong pass");	
				fclose(librecord);
				
        		fclose(temp);
        		remove("temp.txt");
				return;
			}
			found = 1;
			}
			else{
				fprintf(temp,"\n%d\t%s\t%s\t%d\t%d\t", book.bid,book.bname,book.author,book.status,book.nooftitles);
            	for(i=0;i<book.nooftitles;i++)
            		fprintf(temp,"%s\t",book.titles); 
         	}
         	    fscanf(librecord,"%d\t%s\t%s\t%d\t%d", &book.bid,&book.bname,&book.author,&book.status,&book.nooftitles);
	
        }
        fclose(librecord);
        fclose(temp);
        remove("librecord.txt");
		rename("temp.txt","librecord.txt");

    }
}
void Exit()
{
  exit(0);
}

